// index.js
// 获取应用实例
var baseEncrypt = require('../../common/base64') // 引入base64加密文件
const app = getApp()

Page({
  data: {
    statusBar: app.globalData.statusBar,
    customBar: app.globalData.customBar,
    custom: app.globalData.custom,
    url: '',
    videoUrl: '',
    video_url: '',
    videoState: false,
    progress: '',
    input_img: false
  },

  onLoad() {

  },
  get_url() {
    if (!this.data.videoUrl) {
      wx.showToast({
        title: '请输入地址',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '解析中...',
    })
    var that = this
    var Url1 = ''
    var re = /(http[s]?:\/\/([\w-]+.)+([:\d+])?(\/[\w-\.\/\?%&=]*)?)/gi;
    that.data.videoUrl.replace(re, function (a) {
      Url1 = a
    });
    var arr = new Array()
    let resultdata = {}
    var timestamp = new Date().getTime()
    timestamp = timestamp.toString(16)
    arr['timestamp'] = timestamp
    // arr['url'] = Url1
    var sign = app.encrypt(arr);
    // 组装data参数
    resultdata['url'] = Url1
    resultdata['timestamp'] = timestamp,
      resultdata['sign'] = sign
    wx.request({
      url: app.siteInfo.jp_shop_host + '/micro/wdjp/shop/douYinLink', //请求地址
      data: resultdata,
      method: "GET", //get为默认方法/POST
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            video_url: res.data.data.link,
            videoState: true
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1500
          })
        }
        wx.hideLoading();
      },
      fail: function (err) {}, //请求失败
    })
  },
  // 保存视频
  save_videos1(e) {
    var that = this
    // wx.showLoading({
    //   title: '下载中...',
    // })
    // console.log(this.data.video_url)
    var task = wx.downloadFile({
      url: that.data.video_url,
      success: function (res) {
        let filePath = res.tempFilePath;
        //正常 同时可以保存视频文件 
        wx.saveVideoToPhotosAlbum({ // 未登录不正常
          'filePath': filePath,
          success: function (res) {
            console.log(res)
            // 成功则返回图片本地路径
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 1500
            })
          },
          fail: function (err) {
            wx.showToast({
              title: '取消保存',
              icon: 'none',
              duration: 1500
            })
            wx.hideLoading();
            console.log('错误码：' + err.errCode);
            console.log('错误信息：' + err.errMsg);
          },
        });
      },
      fail: function (e) {
        console.log(e)
        wx.showToast({
          title: '保存失败，请稍后再试',
          icon: 'none',
          duration: 2000
        })
      }
    })
    task.onProgressUpdate((res) => {
      if (res.progress === 100) {
        that.setData({
          progress: ''
        })
        // wx.hideLoading();
      } else {
        that.setData({
          progress: res.progress + '%'
        })
      }
    })
  },
  bindKeyFocus(e) {
    if (this.data.videoUrl != '') {
      this.setData({
        input_img: true
      })
    } else {
      this.setData({
        input_img: false
      })
    }
  },
  // bindKeyBlur(e) {
  //   console.log(e)
  //   this.setData({
  //     input_img: false
  //   })
  // },
  delete_input() {
    this.setData({
      videoUrl: '',
      input_img: false
    })
  },
  // 点击复制链接
  copyLink(e) {
    wx.setClipboardData({
      data: this.data.video_url,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {}
        })
      }
    })
  },
  bindModel(e) {
    this.setData({
      videoUrl: e.detail.value
    });
    if (this.data.videoUrl != '') {
      this.setData({
        input_img: true
      })
    } else {
      this.setData({
        input_img: false
      })
    }
  },
  onShow() {
    var that = this
    wx.getClipboardData({
      success(res) {
        wx.hideToast();
        var re = /(http[s]?:\/\/([\w-]+.)+([:\d+])?(\/[\w-\.\/\?%&=]*)?)/gi;
        res.data.replace(re, function (a) {
          that.setData({
            url: a
          })
        });
        if (that.data.url) {
          wx.showModal({
            title: '检测到视频链接确定去水印？',
            content: that.data.url,
            success: function (sm) {
              if (sm.confirm) {
                that.setData({
                  videoUrl: that.data.url
                })
                if (that.data.videoUrl != '') {
                  that.setData({
                    input_img: true
                  })
                } else {
                  that.setData({
                    input_img: false
                  })
                }
                that.get_url()
                // 用户点击了确定 可以调用删除方法了
              } else if (sm.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '匠品去水印工具',
      path: '',
      success: function (res) {}
    }
  },
})